package com.buzz.middleware.dynamic.thread.pool.sdk.registry.redis;

import com.buzz.middleware.dynamic.thread.pool.sdk.domain.model.ThreadPoolConfigEntity;
import com.buzz.middleware.dynamic.thread.pool.sdk.domain.valobj.RegistryEnumVO;
import com.buzz.middleware.dynamic.thread.pool.sdk.registry.IRegistry;
import lombok.AllArgsConstructor;
import org.redisson.api.RBucket;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;

import java.util.List;

/**
 * @Author: LiBaoxuan
 * @CreateTime: 2025-01-04  21:13
 * @Description: TODO
 */
@AllArgsConstructor
public class RedisRegistry implements IRegistry {

    private final RedissonClient dynamicThreadPoolRedissonClient;

    @Override
    public void reportThreadPool(List<ThreadPoolConfigEntity> threadPoolEntities) {
        RList<ThreadPoolConfigEntity> list = dynamicThreadPoolRedissonClient.getList(RegistryEnumVO.THREAD_POOL_CONFIG_LIST_KEY.getKey());
        // todo: 去重
        list.delete();
        list.addAll(threadPoolEntities);
    }

    @Override
    public void reportThreadPoolConfigParameter(ThreadPoolConfigEntity threadPoolConfigEntity) {
        String cacheKey = RegistryEnumVO.THREAD_POOL_CONFIG_PARAMETER_LIST_KEY.getKey() + "_" + threadPoolConfigEntity.getAppName() + "_" + threadPoolConfigEntity.getThreadPoolName();
        RBucket<Object> bucket = dynamicThreadPoolRedissonClient.getBucket(cacheKey);
        bucket.set(threadPoolConfigEntity);
    }

}
